package com.hspedu.array_;

import java.util.Comparator;

/**
 * @author DL5O
 * @version 1.0
 */

/*
案例：自定义 Book 类，里面包含 name 和 price，按 price 排序(从大到小)。
要求使用两种方式排序 , 有一个 Book[] books = 4 本书对象.
使用前面学习过的传递 实现 Comparator 接口匿名内部类，也称为定制排序。
[同学们完成这个即可 10min ]
可以按照 price (1)从大到小 (2)从小到大 (3) 按照书名长度从大到
 */
public class ArraysExercise {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦", 100);
        books[1] = new Book("金瓶梅", 90);
        books[2] = new Book("青年文摘", 5);
        books[3] = new Book("java从入门到放弃", 300);

        System.out.println("===按照书名的价格排序===");
        Book.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book d1 = (Book) o1;
                Book d2 = (Book) o2;
                double ret = d2.getPrice() - d1.getPrice();//按照价格从大到小排
//                double ret = d1.getPrice() - d2.getPrice();//按照价格从小到大排
//                return (int)ret;
//                以上代码有BUG,若价格差0.几的时候转成int会变成0，变为0就不会换位置，
                //升序排序：从大到小
                if (ret > 0) {
                    return -1;
                } else if (ret < 0) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });

        Book.printArr(books);


        //按照书名的长度来进行排序
        System.out.println("===按照书名的长度排序===");
        Book.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book d1 = (Book) o1;
                Book d2 = (Book) o2;
                //按照书名的长度从大到小
                int ret = d2.getName().length() - d1.getName().length();
                return ret;
            }
        });
        Book.printArr(books);
    }
}

class Book {
    private String name;
    private double price;

    public Book(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public static void sort(Book[] arr, Comparator c) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //可以从大到小排，也可以从小到大排，这取决于Comparable的返回值
             /*   if((c.compare(arr[j].price,arr[j+1].price))>0){
                    Book temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }*/

               /* //按照书名
                if((c.compare(arr[j].name.length(),arr[j+1].name.length()))>0){
                    Book temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }*/

                if ((c.compare(arr[j], arr[j + 1])) > 0) {
                    Book temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public static void  printArr(Book[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i].toString());
        }
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}